import { IResponsibilityChain, IHandler } from "../Interfaces/IDesignMode";
/**
 * 责任链基类
 */
export default class ResponsibilityChain implements IResponsibilityChain {
  constructor() {
    this.handlerPipeline = [];
  }
  handlerPipeline: IHandler[];

  addNext(handler: IHandler) {
    this.handlerPipeline.push(handler);
  }

  remove(handler: IHandler) {
    var index = this.handlerPipeline.indexOf(handler);
    if (index > -1) {
      this.handlerPipeline.slice(index, 1);
    }
  }
  invoke(context: any) {
    for (let i = 0; i < this.handlerPipeline.length; i++) {
      const handler = this.handlerPipeline[i];
      //处理工作是否完成-完成则停止请求链
      if (handler.invoke(context)) {
        return;
      }
    }
  }
}
